package com.yixing.demo.controller;

import com.yixing.demo.constants.Constants;
import com.yixing.demo.model.Friend;
import com.yixing.demo.model.User;
import com.yixing.demo.service.FriendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/friend")
@RestController
@Slf4j
public class FriendController {
    @Resource
    FriendService friendService;
    @RequestMapping("/getfriendList")
    public Object getFriendList(HttpServletRequest req) {
        log.info("开始获取好友列表");
        HttpSession session = req.getSession(false);
        if(session == null) {
            log.info("获取不到session对象");
            return new ArrayList<Friend>();
        }
        User userInfo = (User)session.getAttribute(Constants.USER_SESSION);
        if(userInfo == null) {
            log.info("获取不到user对象");
            return new ArrayList<Friend>();
        }
        return friendService.getFriendListByUserId(userInfo.getUserId());
    }
}
